package com.xingxing.Dao;

import com.xingxing.Bean.User;
import com.xingxing.Util.C3P0Util;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;

public class UserDao {

    public static boolean insert(User user) {
        int num = 0;
        Connection conn = null;
        try {
            conn = C3P0Util.getConnection();
            QueryRunner runner = new QueryRunner();
            String sql = "insert into User values(?,?,?,?,?,?,?,?);";
            num = runner.update(conn, sql, new Object[]{
                    user.getUserAccount(),
                    user.getNickName(),
                    user.getUserPassword(),
                    user.getUserSex(),
                    user.getBornDate(),
                    user.getUserRegion(),
                    user.getUserPhone(),
                    user.getIntroduce()
            });
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (num > 0) return true;
        return false;
    }

    public static ArrayList<User> findAll() {
        ArrayList list = null;
        try {
            QueryRunner runner = new QueryRunner();
            String sql = "select * from User";
            list = (ArrayList) runner.query(C3P0Util.getConnection(), sql, new BeanListHandler(User.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public static User find(String userAccount) {
        User user = null;
        try {
            QueryRunner runner = new QueryRunner();
            String sql = "select * from User where userAccount = ?";
            user = (User) runner.query(C3P0Util.getConnection(), sql, new BeanHandler(User.class), new Object[]{userAccount});
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }

    public static boolean updateNickName(User user) {
        try {
            QueryRunner runner = new QueryRunner(C3P0Util.getDataSource());
            String sql = "update User set nickName= ? where userAccount = ?";
            if (runner.update(sql, new Object[]{user.getNickName(), user.getUserAccount()}) > 0) return true;
            return false;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean updatePassword(User user) {
        try {
            QueryRunner runner = new QueryRunner(C3P0Util.getDataSource());
            String sql = "update User set userPassword= ? where userAccount = ?";
            if (runner.update(sql, new Object[]{user.getUserPassword(), user.getUserAccount()}) > 0) return true;
            return false;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean delete(String userAccount) {
        try {
            QueryRunner runner = new QueryRunner(C3P0Util.getDataSource());
            String sql = "delete from User where userAccount = ?";
            if (runner.update(sql, new Object[]{userAccount}) > 0) return true;
            return false;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}

